﻿using System.Collections.Generic;
using System.Data.SqlClient;
using Abs.Core.Models.Room;

namespace Abs.Core.DAL.AdoNet
{
    internal class RoomRepository : AdoNetRepository<Room>, IIdentity
    {
        public override bool Create(Room item)
        {
            var name = new SqlParameter("Name", item.Name);

            this.SqlCommand.CommandText = StoredProcedures.CreateRoom;
            this.SqlCommand.Parameters.Add(name);

            return this.ExecuteNonQuery();
        }//end of public override bool Create(Room item)
        
        public override IList<Room> SelectAll()
        {
            var rooms = new List<Room>();
            this.SqlCommand.CommandText = StoredProcedures.GetRooms;
            var dataReader = this.ExecuteReader();

            if( dataReader == null )
                return rooms;

            while (dataReader.Read())
            {
                rooms.Add(new Room
                {
                    RoomId = (int) dataReader["RoomId"],
                    Name = (string)dataReader["Name"],
                });
            }//end of if (dataReader.Read() == true)
            return rooms;
        }//end of public override IList<Room> SelectAll()

        /// <summary>
        ///     Get Room based on the given RoomId as primary key.
        /// </summary>
        /// <param name="id">
        ///     Primary Key
        ///     id[0] represents "RoomId".
        /// </param>
        /// <returns>
        ///     Return an user, if successfully deleting the item.
        ///     Otherwise return null.
        /// </returns>
        public override Room Select(object[] id)
        {
            var roomId = new SqlParameter("RoomId", id[0]);

            this.SqlCommand.CommandText = StoredProcedures.GetRoom;
            this.SqlCommand.Parameters.Add(roomId);

            var dataReader = this.ExecuteReader();

            if( dataReader == null )
                return null;
            
            if (dataReader.Read())
            {
                return new Room
                {
                    RoomId = (int) dataReader["RoomId"],
                    Name = (string)dataReader["Name"],
                };
            }//end of if (dataReader.Read() == true)
            return null;
        }//end of public override Room Select(object[] id)

        public override bool Update(Room item)
        {
            var name = new SqlParameter("Name", item.Name);
            var roomId = new SqlParameter("RoomId", item.RoomId);

            this.SqlCommand.CommandText = StoredProcedures.EditRoom;
            this.SqlCommand.Parameters.Add(name);
            this.SqlCommand.Parameters.Add(roomId);

            return this.ExecuteNonQuery();
        }//end of public override bool Update(Room item)

        /// <summary>
        ///     Delete Room based on the given RoomId as primary key.
        /// </summary>
        /// <param name="id">
        ///     Primary Key
        ///     id[0] represents "RoomId".
        /// </param>
        /// <returns>
        ///     Return true, if successfully deleting the item.
        ///     Otherwise return false.
        /// </returns>
        public override bool Delete(object[] id)
        {
            var roomId = new SqlParameter("RoomId", id[0]);

            this.SqlCommand.CommandText = StoredProcedures.DeleteRoom;
            this.SqlCommand.Parameters.Add(roomId);
           
            return this.ExecuteNonQuery();
        }//end of public override bool Delete(object[] id)
        public int Identity
        {
            get
            {
                return this.GetIdentity(Tables.RoomTable);
            }
        }//end of public int GetIdentity()
    }//end of public class RoomRepository
}//end of namespace ABS.DAL.AdoNet